<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/5/15
 * Time: 15:59
 */

namespace app\admin\logic;

use app\admin\logic\code\ErrorCode;
use app\common\model\User;
use app\vehicle\model\UserIntegral;
use app\common\model\UserInfo;
class UsersLogic extends BaseLogic
{

    private $usersModel;

    public function __construct()
    {
        parent::__construct();
        $this->usersModel = new User();
        $this->integralModel = new UserIntegral();

    }
    public function userSiteList($tempWhere, $nowPage){
        $where[] = ['site_time','>',0];
        if ($tempWhere['status'] && $tempWhere['status'] >= 0){
            $where[] = ['status', '=', $tempWhere['status']];
        }
        if ($tempWhere['name']){
            $where[] = ['nick_name', 'LIKE', "%".$tempWhere['name']."%"];
        }

        $count = $this->usersModel->where($where)->count();
        $list = $this->usersModel->with('siteName')->order('id desc')->where($where)->page($nowPage, $tempWhere['limit'])->select();

        if ($count > 0){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $list, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    public function userList($tempWhere, $nowPage){
        if ($tempWhere['status'] && $tempWhere['status'] >= 0){
            $where[] = ['status', '=', $tempWhere['status']];
        }
        if ($tempWhere['name']){
            $where[] = ['nick_name', 'LIKE', "%".$tempWhere['name']."%"];
        }
        if ($tempWhere['mobile']){
            $where[] = ['mobile', 'LIKE', "%".$tempWhere['name']."%"];
        }
        if (is_numeric($tempWhere['is_party'])){
            $where[] = ['is_party', 'eq', $tempWhere['is_party'] ];
        }
        $count = $this->usersModel->where($where)->count();
        $list = $this->usersModel->hidden(['password','salt'])->with('info')->order('id desc')->where($where)->page($nowPage, $tempWhere['limit'])->select();
        if ($count > 0){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $list, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    public function userIntegral($data,$nowPage){
        $where = '';
        $role = session('admin_info');
        if($data['id']){
            $where['user_id'] = $data['id'];
        }
        if($role['role_id']==3) {
            $where['user_id'] = $role['shop_id'];
        }
        $count = $this->integralModel->where($where)->count();
        $list = $this->integralModel->where($where)->page($nowPage, self::$pageNum)->select();
        foreach($list as $k=>$v){
            $user = $this->usersModel->where('id','eq',$v['user_id'])->find();
            $list[$k]['head_pic'] = $user['head_pic'];
            $list[$k]['nick_name'] = $user['nick_name'];
            $type = $v['type_id']==1?'-':'+';
            $list[$k]['integral'] = $type.$v['integral'];
        }
        if ($count > 0){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $list, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    public function auditList($tempWhere, $nowPage){
        $role = session('admin_info');
        $where[] = ['id_card', 'neq', ''];
        if($role['role_id']==3) {
            $where[] = ['id', 'eq', $role['shop_id']];
        }
        if (is_numeric($tempWhere['status'])){
            $where[] = ['status', '=', $tempWhere['status']];
        }
        if ($tempWhere['name']){
            $where[] = ['nick_name', 'LIKE', "%".$tempWhere['name']."%"];
        }
        if ($tempWhere['mobile']){
            $where[] = ['mobile', 'LIKE', "%".$tempWhere['name']."%"];
        }
        $count = $this->usersModel->where($where)->count();
        $list = $this->usersModel->where($where)->page($nowPage, self::$pageNum)->select();
        if ($count > 0){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $list, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    public function userAuditList($tempWhere, $nowPage){
        $where[] = ['status', '=', 1];
        if ($tempWhere['name']){
            $where[] = ['nick_name', 'LIKE', "%".$tempWhere['name']."%"];
        }
        $count = UserInfo::where($where)->count();
        $list = UserInfo::where($where)->page($nowPage, self::$pageNum)->select();
        if ($count > 0){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $list, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    public function userInfo($id){
        $userInfo =  User::field('*,status as sid')->where('id','eq',$id)->find();
        if ($userInfo){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $userInfo);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '该信息不存在');
        }
    }

    public function auditInfo($id){
        $userInfo = UserInfo::field('*,status as sid')->where('id','eq',$id)->find();
        if ($userInfo){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $userInfo);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '该信息不存在');
        }
    }

    /**
     * 用户编辑
     * @param $data
     * @return mixed
     */
    public function handle($data){
        $act = $data['act'];
        unset($data['act']);
        $res = false;
        switch ($act){
            case 'add':
                break;
            case 'edit':
                //修改用户资料
                $validate = new \app\admin\validate\User();
                if (!$validate->scene('edit')->check($data)){
                    return ajaxArr(ErrorCode::$VALIDATEERROR, $validate->getError());
                }
                $userInfo = User::get($data['id']);
//                if(!$userInfo->salt){
//                    //设置密码盐
//                    $userInfo->salt = getSalt();
//                }
//                $userInfo->password = pwdMd5($data['password'], $userInfo->salt);
                $userInfo->mobile = $data['mobile'];
                $userInfo->status = $data['status'];
                $userInfo->is_admin = $data['is_admin'];
                $res = $userInfo->save();
                break;
            default:
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }
    public function siteHandle($data){
        $up['site_status'] = $data['site_status'];
        $up['site_id'] = $data['site_id'];
        $up['update_time'] = time();
        $res = User::where('id','eq',$data['id'])->update($up);
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }
    public function auditHandle($data){
        $up['status'] = $data['status'];
        $res = UserInfo::where('id','eq',$data['id'])->update($up);
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }

}